package com.insurance.modules.claim.controller;

import cn.hutool.core.bean.BeanUtil;
import com.insurance.common.common.ResponseData;
import com.insurance.common.utils.R;
import com.insurance.modules.claim.constants.AttachmentEnum;
import com.insurance.modules.claim.constants.ClaimConstants;
import com.insurance.modules.claim.entity.ClaimCaseEntity;
import com.insurance.modules.claim.entity.ClaimSettlementEntity;
import com.insurance.modules.claim.entity.FileInfoEntity;
import com.insurance.modules.claim.entity.SettlementPayEntity;
import com.insurance.modules.claim.service.ClaimCaseService;
import com.insurance.modules.claim.service.ClaimSettlementService;
import com.insurance.modules.claim.service.FileInfoService;
import com.insurance.modules.claim.service.SettlementPayService;
import com.insurance.modules.claim.vo.request.SettlementPaySaveReq;
import com.insurance.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


/**
 * 理赔支付表
 *
 * @author weiruifeng
 * @email
 * @date 2021-09-24 17:42:47
 */
@RestController
@RequestMapping("claim/settlementpay")
@Api(tags = "理赔支付")
public class SettlementPayController extends AbstractController {
    @Autowired
    private SettlementPayService settlementPayService;
    @Autowired
    private ClaimSettlementService claimSettlementService;
    @Autowired
    private ClaimCaseService claimCaseService;
    @Autowired
    private FileInfoService fileInfoService;

    /**
     * 信息
     */
    @RequestMapping("/info/{csId}")
    //@RequiresPermissions("claim:settlementpay:info")
    @ApiOperation(value = "查询信息", notes = "根据id查询信息")
    public ResponseData<SettlementPaySaveReq> info(@PathVariable(value = "csId") String csId) {
        SettlementPayEntity settlementPay = settlementPayService.getByCsId(csId);
        SettlementPaySaveReq rsp = new SettlementPaySaveReq();
        BeanUtil.copyProperties(settlementPay, rsp);
        List<FileInfoEntity> files = fileInfoService.listByBusinessIdAndAttachment(csId, AttachmentEnum.paymentVoucher, null);
        rsp.setFiles(files);
        return ResponseData.success(rsp);
    }

    /**
     * 保存--
     */
    @RequestMapping("/save")
    //@RequiresPermissions("claim:settlementpay:save")
    @ApiOperation(value = "保存或更改信息", notes = "保存或更改信息")
    @Transactional
    public R save(@RequestBody SettlementPaySaveReq settlementPay) {
        Date date = new Date();
        if(StringUtils.isBlank(settlementPay.getId())){
            settlementPay.setCreateBy(getUserId());
            settlementPay.setCreateTime(date);
        }
        settlementPay.setUpdateBy(getUserId());
        settlementPay.setUpdateTime(date);
        settlementPayService.saveOrUpdate(settlementPay);
        ClaimSettlementEntity claimSettlement = new ClaimSettlementEntity();
        claimSettlement.setId(settlementPay.getCsId());
        claimSettlement.setCaseStatus(ClaimConstants.CLAIM_STATUS_FINISH);
        claimSettlement.setUpdateTime(date);
        claimSettlement.setUpdateBy(getUserId());
        claimSettlementService.updateById(claimSettlement);
        ClaimSettlementEntity settlement = claimSettlementService.getById(claimSettlement.getId());
        ClaimCaseEntity caseEntity = new ClaimCaseEntity();
        caseEntity.setId(settlement.getCaseId());
        if(ClaimConstants.CLAIM_STATUS_ACCEPT.equals(settlement.getSettlementOrder())){
            caseEntity.setFirstStatus(settlement.getCaseStatus());
            caseEntity.setCaseStatus(ClaimConstants.CASE_STATUS_FINISH);
        }else{
            caseEntity.setSecondStatus(settlement.getCaseStatus());
        }
        claimCaseService.updateById(caseEntity);

        List<FileInfoEntity> files = settlementPay.getFiles();
        if(CollectionUtils.isNotEmpty(files)){
            for (FileInfoEntity file : files) {
                file.setBusinessId(settlementPay.getCsId());
                file.setCreateTime(date);
                file.setCreateBy(getUserId());
            }
            //批量保存
            fileInfoService.saveBatch(files);
        }
        List<String> deleteFiles = settlementPay.getDeleteFiles();
        if(CollectionUtils.isNotEmpty(deleteFiles)){
            //根据id批量删除
            fileInfoService.removeByIds(deleteFiles);
        }
        return R.ok();
    }

}
